package org.tony.nio.demo.channels;

import java.io.FileInputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

public class ChannelTransferDemo
{

	public static void main(String[] args) throws Exception
	{
		if (args.length == 0)
		{
			System.out.println("Usage: fileName ..");
		}
		catFiles(Channels.newChannel(System.out), args);
	}

	public static void catFiles(WritableByteChannel target , String files[]) throws Exception
	{
		for (int i = 0; i < files.length; i++)
		{
			FileInputStream fis = new FileInputStream(files[i]);
			FileChannel fc = fis.getChannel();
			fc.transferTo(0, fc.size(), target);
			fc.close();
			fis.close();
		}
		target.close();
	}

}
